package com.lei.baweiliao.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.baidu.platform.comapi.map.A;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lei.baweiliao.R;
import com.lei.baweiliao.adapter.CommonAdapter;
import com.lei.baweiliao.utils.SpUtils;
import com.lei.baweiliao.view.fragment.HuihuaFragment;
import com.lei.baweiliao.view.fragment.MeFragment;
import com.lei.baweiliao.view.fragment.TongxunFragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private CommonTabLayout main_common;
    private FrameLayout main_fl;
    private ArrayList<CustomTabEntity> tabEntitys = new ArrayList<>();
    private HuihuaFragment huihuaFragment;
    private TongxunFragment tongxunFragment;
    private MeFragment meFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        tabEntitys.add(new CommonAdapter("会话"));
        tabEntitys.add(new CommonAdapter("通讯录"));
        tabEntitys.add(new CommonAdapter("我"));

        main_common.setTabData(tabEntitys);
        switchFragment(huihuaFragment);
        main_common.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (position){
                    case 0:
                        switchFragment(huihuaFragment);
                        break;
                    case 1:
                        switchFragment(tongxunFragment);
                        break;
                    case 2:
                        switchFragment(meFragment);
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void switchFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.main_fl,fragment)
                .commit();
    }

    private void initView() {
        main_common = (CommonTabLayout) findViewById(R.id.main_common);
        main_fl = (FrameLayout) findViewById(R.id.main_fl);
        huihuaFragment = new HuihuaFragment();
        tongxunFragment = new TongxunFragment();
        meFragment = new MeFragment();
    }
}
